
package com.SimpleAndroidORMTest1;
	
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
	
public class DBHelper extends SQLiteOpenHelper {
	
	public DBHelper(Context context) {
		super(context, "database.db", null, 4);
	}
	
		private boolean tableExists(SQLiteDatabase db, String tablename) {
		String sqlString = "SELECT name FROM sqlite_master WHERE type='table' AND name='" + tablename + "'";
		Cursor c = db.rawQuery(sqlString, null);

		try {
		boolean thereAreRows = (c.getCount() > 0);
		return thereAreRows;
		}
		finally {
		c.close();
		c = null;
		}
		}

		private void createTable(SQLiteDatabase db, String tableName) {
		if (tableExists(db, tableName) == false) {
		String sqlString =
		"CREATE TABLE " + tableName + " ( " +
		"_id INTEGER PRIMARY KEY, " +
		"_nullColumnHack);";
		db.execSQL(sqlString);
		}
		}

		private void createColumn(SQLiteDatabase db, String tableName, String columnName, String defaultValue) {
		String query = "ALTER TABLE " + tableName + " ADD COLUMN " + columnName;
		if (defaultValue != "")
			query += " DEFAULT " + defaultValue;
		db.execSQL(query);
		}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		onUpgrade(db, 0, 1);
	}
	
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		
			
		if ((oldVersion < 1) && (1 <= newVersion)) {
			createTable(db, "SimpleClass");

			
			createColumn(db, "SimpleClass",  "shortValue", "1");
			
			createColumn(db, "SimpleClass",  "shortObject", "");
			
			createColumn(db, "SimpleClass",  "byteValue", "2");
			
			createColumn(db, "SimpleClass",  "byteObject", "");
			
			createColumn(db, "SimpleClass",  "longValue", "3");
			
			createColumn(db, "SimpleClass",  "longObject", "");
			
			createColumn(db, "SimpleClass",  "floatValue", "4");
			
			createColumn(db, "SimpleClass",  "floatObject", "");
			
			createColumn(db, "SimpleClass",  "booleanValue", "1");
			
			createColumn(db, "SimpleClass",  "booleanObject", "");
			
			createColumn(db, "SimpleClass",  "charValue", "'a'");
			
			createColumn(db, "SimpleClass",  "charObject", "");
			
			createColumn(db, "SimpleClass",  "doubleValue", "5");
			
			createColumn(db, "SimpleClass",  "doubleObject", "");
			
			createColumn(db, "SimpleClass",  "intValue", "6");
			
			createColumn(db, "SimpleClass",  "intObject", "");
			
		}
		
	}
	
	public final static class SimpleClassHelper  {
		public static HashMap<Long, SimpleClass> Cache = new HashMap<Long,SimpleClass>();
			

		public static final int Delete(SQLiteDatabase db, SimpleClass value) {
		
			return db.delete("SimpleClass", "_id=?", new String[] { value.getId().toString() });
		}

	
		public static final long Save(SQLiteDatabase db, SimpleClass value) {
			ContentValues values = new ContentValues();

		

			values.put("shortValue", value.getShortValue());
		

			values.put("shortObject", value.getShortObject());
		

			values.put("byteValue", value.getByteValue());
		

			values.put("byteObject", value.getByteObject());
		

			values.put("longValue", value.getLongValue());
		

			values.put("longObject", value.getLongObject());
		

			values.put("floatValue", value.getFloatValue());
		

			values.put("floatObject", value.getFloatObject());
		

			values.put("booleanValue", value.isBooleanValue());
		

			values.put("booleanObject", value.getBooleanObject());
		

			values.put("charValue", String.valueOf(value.getCharValue()));
		

			values.put("charObject", String.valueOf(value.getCharObject()));
		

			values.put("doubleValue", value.getDoubleValue());
		

			values.put("doubleObject", value.getDoubleObject());
		

			values.put("intValue", value.getIntValue());
		

			values.put("intObject", value.getIntObject());
		

			if (value.getId() != null) {
				values.put("_id", value.getId());
			}
			long returnValue = db.replace("SimpleClass", "_nullColumnHack", values);
			value.setId(returnValue);

		

			return returnValue;
		}
	
		public final static class Query {
		
			public static final List<SimpleClass> SelectAll(SQLiteDatabase db) {
				String queryString = "SELECT * FROM SimpleClass";
				Cursor queryCursor = db.rawQuery(queryString, null);
				return getFromCursor(db, queryCursor);
			}
	

			public static final SimpleClass GetById(SQLiteDatabase db, long id) {
				if (Cache.containsKey(id))
					return Cache.get(id);

				String queryString = "SELECT * FROM SimpleClass WHERE _id = " + id;
				Cursor queryCursor = db.rawQuery(queryString, null);
				List<SimpleClass> objects = getFromCursor(db, queryCursor);

				SimpleClass returnObject;

				if (objects.size() == 0) {
					returnObject = null;
				} else {
					returnObject = objects.get(0);
				}

				Cache.put(id, returnObject);
				return returnObject;
			}
	
		
			private static List<SimpleClass> getFromCursor(SQLiteDatabase db, Cursor c) {
				List<SimpleClass> returnValues = new ArrayList<SimpleClass>();
				if (c != null) {
					int _tableIdIndex = c.getColumnIndex("_id");

					
			int shortValueIndex = c.getColumnIndex("shortValue");
		
			int shortObjectIndex = c.getColumnIndex("shortObject");
		
			int byteValueIndex = c.getColumnIndex("byteValue");
		
			int byteObjectIndex = c.getColumnIndex("byteObject");
		
			int longValueIndex = c.getColumnIndex("longValue");
		
			int longObjectIndex = c.getColumnIndex("longObject");
		
			int floatValueIndex = c.getColumnIndex("floatValue");
		
			int floatObjectIndex = c.getColumnIndex("floatObject");
		
			int booleanValueIndex = c.getColumnIndex("booleanValue");
		
			int booleanObjectIndex = c.getColumnIndex("booleanObject");
		
			int charValueIndex = c.getColumnIndex("charValue");
		
			int charObjectIndex = c.getColumnIndex("charObject");
		
			int doubleValueIndex = c.getColumnIndex("doubleValue");
		
			int doubleObjectIndex = c.getColumnIndex("doubleObject");
		
			int intValueIndex = c.getColumnIndex("intValue");
		
			int intObjectIndex = c.getColumnIndex("intObject");
		

		while (c.moveToNext()) {
		SimpleClass newValue = new SimpleClass();
		long objectId = c.getLong(_tableIdIndex);
		newValue.setId(objectId);
		Cache.put(objectId, newValue);

		
					
					newValue.setShortValue(
								c.getShort(shortValueIndex)
							);
				
				if (c.isNull(shortObjectIndex))
					newValue.setShortObject(null);
				else
			
					
					newValue.setShortObject(
								c.getShort(shortObjectIndex)
							);
				
					
					newValue.setByteValue(
								(byte)c.getInt(byteValueIndex)
							);
				
				if (c.isNull(byteObjectIndex))
					newValue.setByteObject(null);
				else
			
					
					newValue.setByteObject(
								(byte)c.getInt(byteObjectIndex)
							);
				
					
					newValue.setLongValue(
								c.getLong(longValueIndex)
							);
				
				if (c.isNull(longObjectIndex))
					newValue.setLongObject(null);
				else
			
					
					newValue.setLongObject(
								c.getLong(longObjectIndex)
							);
				
					
					newValue.setFloatValue(
								c.getFloat(floatValueIndex)
							);
				
				if (c.isNull(floatObjectIndex))
					newValue.setFloatObject(null);
				else
			
					
					newValue.setFloatObject(
								c.getFloat(floatObjectIndex)
							);
				
					
					newValue.setBooleanValue(
								c.getInt(booleanValueIndex) == 1
							);
				
				if (c.isNull(booleanObjectIndex))
					newValue.setBooleanObject(null);
				else
			
					
					newValue.setBooleanObject(
								c.getInt(booleanObjectIndex) == 1
							);
				
					
					newValue.setCharValue(
								c.getString(charValueIndex).charAt(0)
							);
				
				if (c.isNull(charObjectIndex))
					newValue.setCharObject(null);
				else
			
					
					newValue.setCharObject(
								c.getString(charObjectIndex).charAt(0)
							);
				
					
					newValue.setDoubleValue(
								c.getDouble(doubleValueIndex)
							);
				
				if (c.isNull(doubleObjectIndex))
					newValue.setDoubleObject(null);
				else
			
					
					newValue.setDoubleObject(
								c.getDouble(doubleObjectIndex)
							);
				
					
					newValue.setIntValue(
								c.getInt(intValueIndex)
							);
				
				if (c.isNull(intObjectIndex))
					newValue.setIntObject(null);
				else
			
					
					newValue.setIntObject(
								c.getInt(intObjectIndex)
							);
				
		returnValues.add(newValue);
		}
	
				}

				return returnValues;
			}

	
		}
	
	}
		
}
	